package com.niu1078.joe.ec.launcher;

import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.nightonke.wowoviewpager.Animation.WoWoAlphaAnimation;
import com.nightonke.wowoviewpager.Animation.WoWoScaleAnimation;
import com.nightonke.wowoviewpager.Animation.WoWoTranslationAnimation;
import com.nightonke.wowoviewpager.Enum.Ease;
import com.nightonke.wowoviewpager.WoWoViewPager;
import com.nightonke.wowoviewpager.WoWoViewPagerAdapter;
import com.niu1078.joe.app.AppManager;
import com.niu1078.joe.app.Joe;
import com.niu1078.joe.delegates.JoeDelegate;
import com.niu1078.joe.ec.R;
import com.niu1078.joe.ec.R2;
import com.niu1078.joe.utils.dimen.DimenUtil;
import com.niu1078.joe.utils.sp.JoeSp;

import butterknife.BindView;

/**
 * author :ywq .
 * time: 2017/12/19:15:33.
 * desc :引导界面
 * action:
 */

public class LauncherScrollDelegate extends JoeDelegate {
    @BindView(R2.id.wowo_viewpager)
    WoWoViewPager wowoViewpager;
    @BindView(R2.id.view_layout)
    View viewLayout;
    @BindView(R2.id.button)
    TextView button;
    @BindView(R2.id.tv_tiaoguo)
    TextView tvTiaoguo;
    @BindView(R2.id.text)
    TextView text;

    @BindView(R2.id.iv1)
    ImageView iv1;

    @BindView(R2.id.iv2)
    ImageView iv2;
    @BindView(R2.id.iv2_1)
    ImageView iv2_1;
    @BindView(R2.id.iv2_2)
    ImageView iv2_2;
    @BindView(R2.id.iv2_3)
    ImageView iv2_3;
    @BindView(R2.id.iv2_4)
    ImageView iv2_4;
    @BindView(R2.id.iv2_5)
    ImageView iv2_5;
    @BindView(R2.id.iv3)
    ImageView iv3;
    @BindView(R2.id.iv3_0)
    ImageView iv3_0;
    @BindView(R2.id.iv3_1)
    ImageView iv3_1;
    @BindView(R2.id.iv3_3)
    ImageView iv3_3;
    @BindView(R2.id.iv3_4)
    ImageView iv3_4;
    @BindView(R2.id.iv4)
    ImageView iv4;
    @BindView(R2.id.iv5)
    ImageView iv5;

    private boolean animationAdded = false;

    @Override
    public Object setLayout() {
        return R.layout.delegate_launcher_scroll;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        wowoViewpager.setAdapter(WoWoViewPagerAdapter.builder()
                .fragmentManager(getFragmentManager())
                .count(5)
                .build()
        );

        wowoViewpager.addTemporarilyInvisibleViews(1, viewLayout, button);
        addAnimations();
        initListener();
    }

    private void initListener() {

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gotoHome();
            }
        });

    }


    private void addAnimations() {
        if (animationAdded) return;
        animationAdded = true;
/**
 * 添加第一个界面的动画
 */
        addPageOneAnimation();
        /**
         * 添加第二个界面的动画
         */
        addPageTwoAnimation();
        /**
         * 添加第三个界面的动画
         */
        addPageThreeAnimation();

        /**
         * 文字动画
         */
        addTextAnimation();
        //按钮
        addButtonAnimation();
        wowoViewpager.ready();


        tvTiaoguo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                跳过
                   gotoHome();

            }
        });
        final String[] texts = {
                "1牛返返app全新升级\n让一切不可能变为可能",
                "2牛返返app全新升级\n让一切不可能变为可能",
                "3牛返返app全新升级\n让一切不可能变为可能"
        };

        // Do this the prevent the edit-text and button views on login layout
        // to intercept the drag event.
        wowoViewpager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
                viewLayout.setEnabled(position == 2);
                viewLayout.setVisibility(position + positionOffset < 2 ? View.INVISIBLE : View.VISIBLE);
                tvTiaoguo.setVisibility(position + positionOffset < 2 ? View.VISIBLE : View.INVISIBLE);
                //设置文字和指示器
                switch (position) {
                    case 0:
                        text.setText(texts[0]);

                        break;

                    case 1:
                        text.setText(texts[1]);


                        break;

                    case 2:
                        text.setText(texts[2]);


                        break;

                    default:
                        break;
                }


            }
        });
    }

    private void gotoHome() {
        Toast.makeText(Joe.getApplicationContext(), "去首页", Toast.LENGTH_SHORT).show();
        //记录本次登录的版本号
        JoeSp.setVersion(AppManager.getVersionCode());
    }

    private void addPageThreeAnimation() {
        Resources resources = getResources();
        //左上角
        wowoViewpager.addAnimation(iv3_3)
                .add(
                        WoWoTranslationAnimation.builder().
                                page(1).fromX(0).toX(resources.getDimensionPixelOffset(R.dimen.dp_183)).
                                fromY(0).toY(resources.getDimensionPixelOffset(R.dimen.dp_242))
                                .ease(Ease.OutBack)
                                .sameEaseBack(false)
                                .build());
        //右上角
        wowoViewpager.addAnimation(iv3_4)
                .add(
                        WoWoTranslationAnimation.builder().
                                page(1).
                                fromY(0).toY(resources.getDimensionPixelOffset(R.dimen.dp_247))
                                .fromX(0).toX(resources.getDimensionPixelOffset(R.dimen.dp_m_178))
                                .ease(Ease.OutBack)
                                .sameEaseBack(false)
                                .build());
        //左边出来的
        wowoViewpager.addAnimation(iv3_1)
                .add(
                        WoWoTranslationAnimation.builder().
                                page(1).fromX(0).toX(resources.getDimensionPixelOffset(R.dimen.dp_m_78)).
                                keepY(0.0)
                                .ease(Ease.OutBack)
                                .sameEaseBack(false)
                                .build());

        wowoViewpager.addAnimation(iv3_0)
                .add(WoWoAlphaAnimation.builder().page(1).from(0.0).to(1.0).build())
                .add(WoWoScaleAnimation.builder().page(1).fromXY(0.0).toXY(1.0).build())

        ;

    }

    private void addPageTwoAnimation() {
//getDimensionPixelOffset
        Resources resources = getResources();

//第二个界面的左上角的动画
        wowoViewpager.addAnimation(iv2_1)
                .add(WoWoTranslationAnimation.builder().
                        page(0).fromX(0).toX(resources.getDimensionPixelOffset(R.dimen.dp_203)).
                        keepY(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoTranslationAnimation.builder().
                        page(1).fromX(resources.getDimensionPixelOffset(R.dimen.dp_203)).toX(0).
                        keepY(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoAlphaAnimation.builder().page(0).from(0.0).to(1.0).build())
                .add(WoWoAlphaAnimation.builder().page(1).from(1.0).to(0.0).build());
//第二个界面上的右边的动画
        wowoViewpager.addAnimation(iv2_2)
                .add(WoWoTranslationAnimation.builder().
                        page(0).fromX(0).toX(resources.getDimensionPixelOffset(R.dimen.dp_m_111)).
                        keepY(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoTranslationAnimation.builder().
                        page(1).fromX(resources.getDimensionPixelOffset(R.dimen.dp_m_111)).toX(0).
                        keepY(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoAlphaAnimation.builder().page(0).from(0.0).to(1.0).build())
                .add(WoWoAlphaAnimation.builder().page(1).from(1.0).to(0.0).build());
        //第二个界面从天而降的动画
        wowoViewpager.addAnimation(iv2_3)
                .add(WoWoTranslationAnimation.builder().
                        page(0).fromY(0).toY(resources.getDimensionPixelOffset(R.dimen.dp_238)).
                        keepX(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoTranslationAnimation.builder().
                        page(1).fromY(resources.getDimensionPixelOffset(R.dimen.dp_238)).toY(0).
                        keepX(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoAlphaAnimation.builder().page(0).from(0.0).to(1.0).build())
                .add(WoWoAlphaAnimation.builder().page(1).from(1.0).to(0.0).build());
        //中间部位隐藏的动画
        wowoViewpager.addAnimation(iv2_4)
                .add(WoWoTranslationAnimation.builder().
                        page(0).fromY(0).toY(resources.getDimensionPixelOffset(R.dimen.dp_100)).
                        keepX(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoTranslationAnimation.builder().
                        page(1).fromY(resources.getDimensionPixelOffset(R.dimen.dp_100)).toY(0).
                        keepX(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoScaleAnimation.builder().page(0).fromXY(0.0).toXY(1.0).build())
                .add(WoWoScaleAnimation.builder().page(1).fromXY(1.0).toXY(0.0).build())
                .add(WoWoAlphaAnimation.builder().page(0).from(0.0).to(1.0).build())
                .add(WoWoAlphaAnimation.builder().page(1).from(0.2).to(0.0).build());

        wowoViewpager.addAnimation(iv2_5)
                .add(WoWoScaleAnimation.builder().page(0).fromXY(0.0).toXY(1.0).build())
                .add(WoWoScaleAnimation.builder().page(1).fromXY(1.0).toXY(0.0).build())
                .add(WoWoAlphaAnimation.builder().page(0).from(0.0).to(1.0).build())
                .add(WoWoAlphaAnimation.builder().page(1).from(0.2).to(0.0).build());
    }

    /**
     * 第一个界面的动画
     */
    private void addPageOneAnimation() {
        Resources resources = getResources();

        //第一个界面上的左上角的图片
        wowoViewpager.addAnimation(iv1)
                //位移动画
                .add(WoWoTranslationAnimation.builder().page(0).fromY(0).toY(-dp2px(277)).keepX(0).ease(Ease.OutBack).sameEaseBack(false).build())
                //渐变动画
                .add(WoWoAlphaAnimation.builder().page(0).from(1).to(0.0).build());

//.第一个界面的右边的动画 需要向右移出屏幕
        wowoViewpager.addAnimation(iv2)
                //向右移动的动画
                .add(WoWoTranslationAnimation.builder().page(0).fromX(0).toX(dp2px(232)).keepY(0).ease(Ease.OutBack).sameEaseBack(false).build())
                //渐变动画
                .add(WoWoAlphaAnimation.builder().page(0).from(1).to(0.0).build());


//第一个界面最中间的动画
        wowoViewpager.addAnimation(iv3)
                //进入第二个界面时候这个是放大的动画
                .add(WoWoScaleAnimation.builder().page(0).fromXY(1).toXY(192.00 / 116.00).build())
                .add(WoWoTranslationAnimation.builder().page(0).fromY(0).toY(resources.getDimensionPixelOffset(R.dimen.dp_m_8)).keepX(0).ease(Ease.OutBack).sameEaseBack(false).build())
                //进入第三个界面的时候这个需要恢复大小
                .add(WoWoScaleAnimation.builder().page(1).fromXY(192.00 / 116.00).toXY(1).build())
                //并且往下移动一些距离
                .add(WoWoTranslationAnimation.builder().page(1).fromY(0).toY(dp2px(178)).keepX(0).ease(Ease.OutBack).sameEaseBack(false).build())
        ;

//第一个界面上的左下方的图片 滑动的时候 向左滑动隐藏
        wowoViewpager.addAnimation(iv4)
                .add(WoWoTranslationAnimation.builder().page(0).fromX(0).toX(-dp2px(235)).keepY(0).ease(Ease.OutBack).sameEaseBack(false).build())
                .add(WoWoAlphaAnimation.builder().page(0).from(1).to(0.0).build())
        ;
        //第一个界面上的右下角的图片 往右移出屏幕
        wowoViewpager.addAnimation(iv5)
                .add(WoWoTranslationAnimation.builder().page(0).fromY(0).toY(dp2px(277)).
                        keepX(0)
                        .ease(Ease.OutBack)
                        .sameEaseBack(false)
                        .build())
                .add(WoWoAlphaAnimation.builder().page(0).from(1).to(0.0).build());

    }

    private void addTextAnimation() {
//        View text = findViewById(R.id.text);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) text.setZ(50);
//        String[] texts = new String[]{
//                "牛返返APP全新升级",
//                "让一切不可能变成可能",
//                "牛返返APP全新升级",
//
//        };
//        wowo.addAnimation(text)
//                .add(WoWoTextViewTextAnimation.builder().page(0).from(texts[0]).to(texts[1]).build())
//                .add(WoWoTextViewTextAnimation.builder().page(1).from(texts[1]).to(texts[2]).build())
//                .add(WoWoTextViewColorAnimation.builder().page(1).from("#05502f").to(Color.BLACK).build());
    }


    private void addButtonAnimation() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) button.setZ(50);
        wowoViewpager.addAnimation(button).add(WoWoAlphaAnimation.builder().page(1).from(0).to(1).build());


    }

    protected int dp2px(float dp) {
        return DimenUtil.dp2px((int) dp);
    }
}
